<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>networktester.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_networktester.js.html">networktester.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of goog.net.NetworkTester.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.net.NetworkTester&#39;);
<a name="line20"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line21"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line22"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Creates an instance of goog.net.NetworkTester which can be used to test
<a name="line28"></a> * for internet connectivity by seeing if an image can be loaded from
<a name="line29"></a> * google.com. It can also be tested with other URLs.
<a name="line30"></a> * @param {Function} callback Callback that is called when the test completes.
<a name="line31"></a> *     The callback takes a single boolean parameter. True indicates the URL
<a name="line32"></a> *     was reachable, false indicates it wasn&#39;t.
<a name="line33"></a> * @param {Object=} opt_handler Handler object for the callback.
<a name="line34"></a> * @param {goog.Uri=} opt_uri URI to use for testing.
<a name="line35"></a> * @constructor
<a name="line36"></a> */
<a name="line37"></a>goog.net.NetworkTester = function(callback, opt_handler, opt_uri) {
<a name="line38"></a>  /**
<a name="line39"></a>   * Callback that is called when the test completes.
<a name="line40"></a>   * The callback takes a single boolean parameter. True indicates the URL was
<a name="line41"></a>   * reachable, false indicates it wasn&#39;t.
<a name="line42"></a>   * @type {Function}
<a name="line43"></a>   * @private
<a name="line44"></a>   */
<a name="line45"></a>  this.callback_ = callback;
<a name="line46"></a>
<a name="line47"></a>  /**
<a name="line48"></a>   * Handler object for the callback.
<a name="line49"></a>   * @type {Object|undefined}
<a name="line50"></a>   * @private
<a name="line51"></a>   */
<a name="line52"></a>  this.handler_ = opt_handler;
<a name="line53"></a>
<a name="line54"></a>  if (!opt_uri) {
<a name="line55"></a>    // set the default URI to be based on the cleardot image at google.com
<a name="line56"></a>    // We need to add a &#39;rand&#39; to make sure the response is not fulfilled
<a name="line57"></a>    // by browser cache. Use protocol-relative URLs to avoid insecure content
<a name="line58"></a>    // warnings in IE.
<a name="line59"></a>    opt_uri = new goog.Uri(&#39;//www.google.com/images/cleardot.gif&#39;);
<a name="line60"></a>    opt_uri.makeUnique();
<a name="line61"></a>  }
<a name="line62"></a>
<a name="line63"></a>  /**
<a name="line64"></a>   * Uri to use for test. Defaults to using an image off of google.com
<a name="line65"></a>   * @type {goog.Uri}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.uri_ = opt_uri;
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Default timeout
<a name="line74"></a> * @type {number}
<a name="line75"></a> */
<a name="line76"></a>goog.net.NetworkTester.DEFAULT_TIMEOUT_MS = 10000;
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Logger object
<a name="line81"></a> * @type {goog.debug.Logger}
<a name="line82"></a> * @private
<a name="line83"></a> */
<a name="line84"></a>goog.net.NetworkTester.prototype.logger_ =
<a name="line85"></a>    goog.debug.Logger.getLogger(&#39;goog.net.NetworkTester&#39;);
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Timeout for test
<a name="line90"></a> * @type {number}
<a name="line91"></a> * @private
<a name="line92"></a> */
<a name="line93"></a>goog.net.NetworkTester.prototype.timeoutMs_ =
<a name="line94"></a>    goog.net.NetworkTester.DEFAULT_TIMEOUT_MS;
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Whether we&#39;ve already started running.
<a name="line99"></a> * @type {boolean}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.net.NetworkTester.prototype.running_ = false;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Number of retries to attempt
<a name="line107"></a> * @type {number}
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.net.NetworkTester.prototype.retries_ = 0;
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Attempt number we&#39;re on
<a name="line115"></a> * @type {number}
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>goog.net.NetworkTester.prototype.attempt_ = 0;
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Pause between retries in milliseconds.
<a name="line123"></a> * @type {number}
<a name="line124"></a> * @private
<a name="line125"></a> */
<a name="line126"></a>goog.net.NetworkTester.prototype.pauseBetweenRetriesMs_ = 0;
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Timer for timeouts.
<a name="line131"></a> * @type {?number}
<a name="line132"></a> * @private
<a name="line133"></a> */
<a name="line134"></a>goog.net.NetworkTester.prototype.timeoutTimer_ = null;
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Timer for pauses between retries.
<a name="line139"></a> * @type {?number}
<a name="line140"></a> * @private
<a name="line141"></a> */
<a name="line142"></a>goog.net.NetworkTester.prototype.pauseTimer_ = null;
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Returns the timeout in milliseconds.
<a name="line147"></a> * @return {number} Timeout in milliseconds.
<a name="line148"></a> */
<a name="line149"></a>goog.net.NetworkTester.prototype.getTimeout = function() {
<a name="line150"></a>  return this.timeoutMs_;
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Sets the timeout in milliseconds.
<a name="line156"></a> * @param {number} timeoutMs Timeout in milliseconds.
<a name="line157"></a> */
<a name="line158"></a>goog.net.NetworkTester.prototype.setTimeout = function(timeoutMs) {
<a name="line159"></a>  this.timeoutMs_ = timeoutMs;
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Returns the numer of retries to attempt.
<a name="line165"></a> * @return {number} Number of retries to attempt.
<a name="line166"></a> */
<a name="line167"></a>goog.net.NetworkTester.prototype.getNumRetries = function() {
<a name="line168"></a>  return this.retries_;
<a name="line169"></a>};
<a name="line170"></a>
<a name="line171"></a>
<a name="line172"></a>/**
<a name="line173"></a> * Sets the timeout in milliseconds.
<a name="line174"></a> * @param {number} retries Number of retries to attempt.
<a name="line175"></a> */
<a name="line176"></a>goog.net.NetworkTester.prototype.setNumRetries = function(retries) {
<a name="line177"></a>  this.retries_ = retries;
<a name="line178"></a>};
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * Returns the pause between retries in milliseconds.
<a name="line183"></a> * @return {number} Pause between retries in milliseconds.
<a name="line184"></a> */
<a name="line185"></a>goog.net.NetworkTester.prototype.getPauseBetweenRetries = function() {
<a name="line186"></a>  return this.pauseBetweenRetriesMs_;
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Sets the pause between retries in milliseconds.
<a name="line192"></a> * @param {number} pauseMs Pause between retries in milliseconds.
<a name="line193"></a> */
<a name="line194"></a>goog.net.NetworkTester.prototype.setPauseBetweenRetries = function(pauseMs) {
<a name="line195"></a>  this.pauseBetweenRetriesMs_ = pauseMs;
<a name="line196"></a>};
<a name="line197"></a>
<a name="line198"></a>
<a name="line199"></a>/**
<a name="line200"></a> * Returns the uri to use for the test.
<a name="line201"></a> * @return {goog.Uri} The uri for the test.
<a name="line202"></a> */
<a name="line203"></a>goog.net.NetworkTester.prototype.getUri = function() {
<a name="line204"></a>  return this.uri_;
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Sets the uri to use for the test.
<a name="line210"></a> * @param {goog.Uri} uri The uri for the test.
<a name="line211"></a> */
<a name="line212"></a>goog.net.NetworkTester.prototype.setUri = function(uri) {
<a name="line213"></a>  this.uri_ = uri;
<a name="line214"></a>};
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * Returns whether the tester is currently running.
<a name="line219"></a> * @return {boolean} True if it&#39;s running, false if it&#39;s not running.
<a name="line220"></a> */
<a name="line221"></a>goog.net.NetworkTester.prototype.isRunning = function() {
<a name="line222"></a>  return this.running_;
<a name="line223"></a>};
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * Starts the process of testing the network.
<a name="line228"></a> */
<a name="line229"></a>goog.net.NetworkTester.prototype.start = function() {
<a name="line230"></a>  if (this.running_) {
<a name="line231"></a>    throw Error(&#39;NetworkTester.start called when already running&#39;);
<a name="line232"></a>  }
<a name="line233"></a>  this.running_ = true;
<a name="line234"></a>
<a name="line235"></a>  this.logger_.info(&#39;Starting&#39;);
<a name="line236"></a>  this.attempt_ = 0;
<a name="line237"></a>  this.startNextAttempt_();
<a name="line238"></a>};
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>/**
<a name="line242"></a> * Stops the testing of the network. This is a noop if not running.
<a name="line243"></a> */
<a name="line244"></a>goog.net.NetworkTester.prototype.stop = function() {
<a name="line245"></a>  this.cleanupCallbacks_();
<a name="line246"></a>  this.running_ = false;
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Starts the next attempt to load an image.
<a name="line252"></a> * @private
<a name="line253"></a> */
<a name="line254"></a>goog.net.NetworkTester.prototype.startNextAttempt_ = function() {
<a name="line255"></a>  this.attempt_++;
<a name="line256"></a>
<a name="line257"></a>  if (goog.net.NetworkTester.getNavigatorOffline_()) {
<a name="line258"></a>    this.logger_.info(&#39;Browser is set to work offline.&#39;);
<a name="line259"></a>    // Call in a timeout to make async like the rest.
<a name="line260"></a>    goog.Timer.callOnce(goog.bind(this.onResult, this, false), 0);
<a name="line261"></a>  } else {
<a name="line262"></a>    this.logger_.info(&#39;Loading image (attempt &#39; + this.attempt_ +
<a name="line263"></a>                      &#39;) at &#39; + this.uri_);
<a name="line264"></a>    this.image_ = new Image();
<a name="line265"></a>    this.image_.onload = goog.bind(this.onImageLoad_, this);
<a name="line266"></a>    this.image_.onerror = goog.bind(this.onImageError_, this);
<a name="line267"></a>    this.image_.onabort = goog.bind(this.onImageAbort_, this);
<a name="line268"></a>
<a name="line269"></a>    this.timeoutTimer_ = goog.Timer.callOnce(this.onImageTimeout_,
<a name="line270"></a>        this.timeoutMs_, this);
<a name="line271"></a>    this.image_.src = String(this.uri_);
<a name="line272"></a>  }
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * @return {boolean} Whether navigator.onLine returns false.
<a name="line278"></a> * @private
<a name="line279"></a> */
<a name="line280"></a>goog.net.NetworkTester.getNavigatorOffline_ = function() {
<a name="line281"></a>  return &#39;onLine&#39; in navigator &amp;&amp; !navigator.onLine;
<a name="line282"></a>};
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * Callback for the image successfully loading.
<a name="line287"></a> * @private
<a name="line288"></a> */
<a name="line289"></a>goog.net.NetworkTester.prototype.onImageLoad_ = function() {
<a name="line290"></a>  this.logger_.info(&#39;Image loaded&#39;);
<a name="line291"></a>  this.onResult(true);
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Callback for the image failing to load.
<a name="line297"></a> * @private
<a name="line298"></a> */
<a name="line299"></a>goog.net.NetworkTester.prototype.onImageError_ = function() {
<a name="line300"></a>  this.logger_.info(&#39;Image load error&#39;);
<a name="line301"></a>  this.onResult(false);
<a name="line302"></a>};
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * Callback for the image load being aborted.
<a name="line307"></a> * @private
<a name="line308"></a> */
<a name="line309"></a>goog.net.NetworkTester.prototype.onImageAbort_ = function() {
<a name="line310"></a>  this.logger_.info(&#39;Image load aborted&#39;);
<a name="line311"></a>  this.onResult(false);
<a name="line312"></a>};
<a name="line313"></a>
<a name="line314"></a>
<a name="line315"></a>/**
<a name="line316"></a> * Callback for the image load timing out.
<a name="line317"></a> * @private
<a name="line318"></a> */
<a name="line319"></a>goog.net.NetworkTester.prototype.onImageTimeout_ = function() {
<a name="line320"></a>  this.logger_.info(&#39;Image load timed out&#39;);
<a name="line321"></a>  this.onResult(false);
<a name="line322"></a>};
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/**
<a name="line326"></a> * Handles a successful or failed result.
<a name="line327"></a> * @param {boolean} succeeded Whether the image load succeeded.
<a name="line328"></a> */
<a name="line329"></a>goog.net.NetworkTester.prototype.onResult = function(succeeded) {
<a name="line330"></a>  this.cleanupCallbacks_();
<a name="line331"></a>
<a name="line332"></a>  if (succeeded) {
<a name="line333"></a>    this.running_ = false;
<a name="line334"></a>    this.callback_.call(this.handler_, true);
<a name="line335"></a>  } else {
<a name="line336"></a>    if (this.attempt_ &lt;= this.retries_) {
<a name="line337"></a>      if (this.pauseBetweenRetriesMs_) {
<a name="line338"></a>        this.pauseTimer_ = goog.Timer.callOnce(this.onPauseFinished_,
<a name="line339"></a>            this.pauseBetweenRetriesMs_, this);
<a name="line340"></a>      } else {
<a name="line341"></a>        this.startNextAttempt_();
<a name="line342"></a>      }
<a name="line343"></a>    } else {
<a name="line344"></a>      this.running_ = false;
<a name="line345"></a>      this.callback_.call(this.handler_, false);
<a name="line346"></a>    }
<a name="line347"></a>  }
<a name="line348"></a>};
<a name="line349"></a>
<a name="line350"></a>
<a name="line351"></a>/**
<a name="line352"></a> * Callback for the pause between retry timer.
<a name="line353"></a> * @private
<a name="line354"></a> */
<a name="line355"></a>goog.net.NetworkTester.prototype.onPauseFinished_ = function() {
<a name="line356"></a>  this.pauseTimer_ = null;
<a name="line357"></a>  this.startNextAttempt_();
<a name="line358"></a>};
<a name="line359"></a>
<a name="line360"></a>
<a name="line361"></a>/**
<a name="line362"></a> * Cleans up the handlers and timer associated with the image.
<a name="line363"></a> * @private
<a name="line364"></a> */
<a name="line365"></a>goog.net.NetworkTester.prototype.cleanupCallbacks_ = function() {
<a name="line366"></a>  // clear handlers to avoid memory leaks
<a name="line367"></a>  // NOTE(user): Nullified individually to avoid compiler warnings
<a name="line368"></a>  // (BUG 658126)
<a name="line369"></a>  if (this.image_) {
<a name="line370"></a>    this.image_.onload = null;
<a name="line371"></a>    this.image_.onerror = null;
<a name="line372"></a>    this.image_.onabort = null;
<a name="line373"></a>    this.image_ = null;
<a name="line374"></a>  }
<a name="line375"></a>  if (this.timeoutTimer_) {
<a name="line376"></a>    goog.Timer.clear(this.timeoutTimer_);
<a name="line377"></a>    this.timeoutTimer_ = null;
<a name="line378"></a>  }
<a name="line379"></a>  if (this.pauseTimer_) {
<a name="line380"></a>    goog.Timer.clear(this.pauseTimer_);
<a name="line381"></a>    this.pauseTimer_ = null;
<a name="line382"></a>  }
<a name="line383"></a>};
</pre>


</body>
</html>
